Hey guys.... a little PHP help please. I'm running this to calculate and display the file size of the attachment in showflat and showthreaded view. BUT.... sometimes... for some reason it leaves the filesize off and just prints the word " Bytes" with no number before it.
Basically it gets the filesize (in bytes)... then displays it as MB, KB or if its not big enough for either of those... it's supposed to display the total in Bytes. But I can't figure out why sometimes it doesn't work right. Any ideas?
code:
$File = rawurlencode($File);
$thefile = "{$config['files']}/$File";
// Define some file sizes
$kb = 1024; // Kilobyte
$mb = 1048576; // Megabyte
// Get the file size in bytes.
$attachmentsize = filesize($thefile);
//If it's less than a kb we just return the size, otherwise we keep going until
//the size is in the appropriate measurement range.
if($attachmentsize > $mb) {
$filesize = round($attachmentsize/$mb,2)." MB";
}
elseif($attachmentsize > $kb) {
$filesize = round($attachmentsize/$kb,2)." KB";
}
else {
$filesize = $attachmentsize." Bytes";
}
Then it plugs the $filesize variable into the post just after the download count.
Thanks for your help.... I'm sure this is a breeze for you "pros".